﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CSharpWin;
using MeTalker.Aide.Forms;

namespace MeTalker.Aide
{
    internal partial class MessageContorl : UserControl
    {

       internal event MouseEventHandler MeMouseClick;
        public MessageContorl()
        {
            InitializeComponent();
            this.meTabControl.Appearance = TabAppearance.Buttons;
            //this.meTabControl.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
            //this.meTabControl.Alignment = System.Windows.Forms.TabAlignment.Top;
            //meTabControl.DrawItem += new DrawItemEventHandler(meTabControl_DrawItem);
            meTabControl.MouseClick+=new MouseEventHandler(meTabControl_MouseClick);
            //this.meTabControl.MouseMove += new MouseEventHandler(meTabControl_MouseMove);
            meTabControl.MouseDown += new MouseEventHandler(meTabControl_MouseDown);
            meTabControl.Click += new EventHandler(meTabControl_Click);
        }

        void meTabControl_Click(object sender, EventArgs e)
        {
            Console.WriteLine("meTabControl_Click");
        }
       

        #region 大
        private int z;
        private Timer a3;
        private int ac;
        TabControlEx ab;
        DateTime aa;

        public static bool b(DateTime A_0, int A_1)
        {
            TimeSpan span = (TimeSpan)(DateTime.Now - A_0);
            long totalMilliseconds = (long)span.TotalMilliseconds;
            return (totalMilliseconds > A_1);
        }

        void b(object A_0, MouseEventArgs A_1)
        {
            if (b(this.aa, 50))
            {
                this.aa = DateTime.Now;
                TabControlEx ex = A_0 as TabControlEx;
                if (ex != null)
                {
                    Point pt = ex.PointToClient(Control.MousePosition);
                    bool flag = false;
                    for (int i = 0; i < ex.TabPages.Count; i++)
                    {
                        Rectangle tabRect = ex.GetTabRect(i);
                        if (tabRect.Contains(pt))
                        {
                            flag = true;
                            if (ex.SelectedIndex != i)
                            {
                                if ((this.z <= tabRect.Top) && (this.z >= tabRect.Bottom))
                                {
                                    ex.SelectedIndex = i;
                                }
                                else
                                {
                                    this.ab = ex;
                                    this.ac = i;
                                    ex.SelectedIndex = i;
                                    Console.WriteLine(i);
                                    //this.a3.Enabled = true;
                                }
                            }
                        }
                    }
                    if (!flag)
                    {
                        //this.z = pt.Y;
                    }
                }
            }
        }
        void meTabControl_MouseMove(object sender, MouseEventArgs e)
        {
            b(sender, e);
        }
        #endregion

        void meTabControl_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    MeTabPage tabpage = meTabControl.SelectedTab as MeTabPage;
                    if (tabpage != null)
                        tabpage.ExpandCollapseAll();
                }
            }
            catch { }
        }

       

        void meTabControl_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                //if()
                TabControl tc = sender as TabControl;
                int tabpage_cnt = tc.TabPages.Count;
                //int new_i = (tc.SelectedIndex + Math.Sign(e.Delta)) % tabpage_cnt;
                int new_i = (int)e.X / 73;// tabpage_cnt;
                if (new_i < 0)
                {
                    new_i = new_i + tabpage_cnt;
                }
                
                //tc.SelectedIndex = new_i;
                //Console.WriteLine(new_i);
            }
            if (MeMouseClick != null)
                MeMouseClick(sender, e);
        }

        void meTabControl_DrawItem(object sender, DrawItemEventArgs e)
        {
            
        }
        internal MeTalker.Aide.Forms.MeTabControlEx TabControl
        {
            get
            {
                return meTabControl;
            }
        }

        private void meTabControl_Selecting(object sender, TabControlCancelEventArgs e)
        {

        }
    }
}
